/*
 * @lc app=leetcode.cn id=290 lang=typescript
 *
 * [290] 单词规律
 */

// @lc code=start

// 主要是使用 hash 重新编码字符，把字符串转化为序号
function wordPattern(pattern: string, s: string): boolean {
    const _pattern = pattern.split("");
    const _s = s.split(" ");
    if (_pattern.length !== _s.length) return false;

    let a = new Map<string, number>();
    let b = new Map<string, number>();
    const setter = (set: Map<string, number>) => {
        return (i: string) => {
            if (!set.has(i)) {
                set.set(i, set.size);
                return set.size;
            }
            return set.get(i)!;
        };
    };
    const SetA = setter(a);
    const SetB = setter(b);

    for (let index = 0; index < _pattern.length; index++) {
        if (SetA(_pattern[index]) !== SetB(_s[index])) return false;
    }
    return true;
}
// @lc code=end
